package top.drinyeit.nongmao.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.presenter.ShopSearchPresenter;

/**
 * Created by Administrator on 2018/2/5.
 */

public class ShopSearchActivity extends BaseActivity{
    public EditText searchKey;
    private TextView back,search;
    public RecyclerView mRecyclerView;
    int storeId;

    private ShopSearchPresenter shopSearchPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_search);

        initView();
        initIntent();
        initData();
        initListener();
        setTitle();
    }

    private void initView(){
        searchKey = findViewById(R.id.et_search_key);
        back = findViewById(R.id.tv_shopSearch_back);
        search = findViewById(R.id.tv_search);
        mRecyclerView = findViewById(R.id.mRecyclerView);
    }

    private void initIntent(){
        if (getIntent() != null){
            storeId = getIntent().getExtras().getInt("store_id");
        }else {
            showToast("网络异常,请重新打开搜索页");
        }
    }

    private void initData(){
        shopSearchPresenter = new ShopSearchPresenter(this);
    }

    private void initListener(){
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
       searchKey.addTextChangedListener(new TextWatcher() {
           @Override
           public void beforeTextChanged(CharSequence s, int start, int count, int after) {

           }

           @Override
           public void onTextChanged(CharSequence s, int start, int before, int count) {

           }

           @Override
           public void afterTextChanged(Editable s) {
               if (searchKey.getText().length() > 0){
                   Map<String,Object> map = new HashMap<>();
                   map.put("action","commodity_category_goods");
                   map.put("store_id",storeId);
                   map.put("search_key",searchKey.getText().toString());
                   shopSearchPresenter.shopSearch(map);
               }
           }
       });
    }
}
